<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="票据本号" prop="ticketno">
        <el-input
          v-model="queryParams.ticketno"
          placeholder="请输入票据本号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领用人" prop="useperson">
        <el-input
          v-model="queryParams.useperson"
          placeholder="请输入领用人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有效" prop="invalid">
         <el-select v-model="queryParams.invalid" placeholder="是否有效" clearable>
          <el-option
              v-for="item in invalidList"
              :key="item.id"
              :label="item.name"
              :value="item.value"
              @change="handleQuery"
            />
        </el-select>
      </el-form-item>
    
      <el-form-item label="是否审核" prop="isaudit">
         <el-select v-model="queryParams.isaudit" placeholder="是否审核" clearable>
          <el-option
              v-for="item in isauditList"
              :key="item.id"
              :label="item.name"
              :value="item.value"
              @change="handleQuery"
            />
        </el-select>
      </el-form-item>

      <el-form-item label="是否核销" prop="isend">
         <el-select v-model="queryParams.isend" placeholder="是否核销" clearable>
          <el-option
              v-for="item in isendList"
              :key="item.id"
              :label="item.name"
              :value="item.value"
              @change="handleQuery"
            />
        </el-select>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:ticket:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house_sale:ticket:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:ticket:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house_sale:ticket:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ticketList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="ticketid" />
      <el-table-column label="票据本号" align="center" prop="ticketno" />
      <el-table-column label="票据类型" align="center" prop="tickettype" />
      <el-table-column label="领用人" align="center" prop="useperson" />
      <el-table-column label="领用时间" align="center" prop="usetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.usetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="票据号起" align="center" prop="startno" />
      <el-table-column label="票据号止" align="center" prop="endno" />
      <el-table-column label="票据号前缀" align="center" prop="prefix" />
      <el-table-column label="是否有效" align="center" prop="invalid" />
      <el-table-column label="购买日期" align="center" prop="buytime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buytime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否审核" align="center" prop="isaudit" />
      <el-table-column label="是否核销" align="center" prop="isend" />
      <!-- <el-table-column label="备注" align="center" prop="demo" /> -->
      <!-- <el-table-column label="${comment}" align="center" prop="nextno" /> -->
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house_sale:ticket:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house_sale:ticket:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改票据管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="票据本号" prop="ticketno">
          <el-input v-model="form.ticketno" placeholder="请输入票据本号" />
        </el-form-item>
        <el-form-item label="票据类型" prop="tickettype">
           <el-select v-model="form.tickettype" >
                <el-option v-for="t in tickettypeList" :label="t.name" :value="t.name" :key="t.id"></el-option>
            </el-select>
          <!-- <el-input v-model="form.tickettype" placeholder="请输入票据本号" /> -->
        </el-form-item>
        <el-form-item label="领用人" prop="useperson">
            <el-input  v-model="form.useperson" placeholder="请选择领用人" style="width:170px"/>
            <el-button size="mini" type="primary" icon="el-icon-s-check" @click.native="selectPerson"></el-button>
          <!-- <el-input v-model="form.useperson" placeholder="请输入领用人" /> -->
        </el-form-item>
        <el-form-item label="领用时间" prop="usetime">
          <el-date-picker clearable
            v-model="form.usetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择领用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="票据号起" prop="startno">
          <el-input v-model="form.startno" placeholder="请输入票据号起" />
        </el-form-item>
        <el-form-item label="票据号止" prop="endno">
          <el-input v-model="form.endno" placeholder="请输入票据号止" />
        </el-form-item>
        <el-form-item label="票据号前缀" prop="prefix">
          <el-input v-model="form.prefix" placeholder="请输入票据号前缀" />
        </el-form-item>
        <el-form-item label="是否有效" prop="invalid">
           <el-select v-model="form.invalid" >
                <el-option v-for="d in invalidList" :label="d.name" :value="d.name" :key="d.id"></el-option>
            </el-select>
          <!-- <el-input v-model="form.invalid" placeholder="请输入是否有效" /> -->
        </el-form-item>
        <el-form-item label="购买日期" prop="buytime">
          <el-date-picker clearable
            v-model="form.buytime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购买日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否审核" prop="isaudit">
           <el-select v-model="form.isaudit" >
                <el-option v-for="a in isauditList" :label="a.name" :value="a.name" :key="a.id"></el-option>
            </el-select>
          <!-- <el-input v-model="form.isaudit" placeholder="请输入是否审核" /> -->
        </el-form-item>
        <el-form-item label="是否核销" prop="isend">
           <el-select v-model="form.isend" >
                <el-option v-for="s in isendList" :label="s.name" :value="s.name" :key="s.id"></el-option>
            </el-select>
          <!-- <el-input v-model="form.isend" placeholder="请输入是否核销" /> -->
        </el-form-item>
        <el-form-item label="备注" prop="demo">
          <el-input v-model="form.demo" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="${comment}" prop="nextno" hidden>
          <el-input v-model="form.nextno" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
     <el-dialog :visible.sync="Person" append-to-body width="250px">
        
             <el-tree
            :indent="30"
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            style="text-decoration:underline;color:blue;"
            default-expand-all
            @node-click="handleChange"
          />
          <div v-show="user"  style="width:200px">
                <el-table v-loading="loading" :data="getUserInfo" :row-class-name="tableRowClassName" @current-change="handleUser">
            <el-table-column label="姓名" align="center" prop="nickName" />
            <el-table-column label="岗位" align="center" prop="remark" />
          </el-table>
          </div>
    </el-dialog>
  </div> 
</template>

<script>
import { listTicket, getTicket, delTicket, addTicket, updateTicket } from "@/api/house_sale/ticket";
import {getUser} from "@/api/house_sale/customer";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Ticket",
  component:[Treeselect],
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "label"
      },
      deptOptions:null,
      user:false,
      Person:false,
      getUserInfo:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 票据管理表格数据
      ticketList: [],
      invalidList:[ {id:0,name:'有效',value:'是'},  {id:1,name:'无效',value:'否'} ],
      isauditList:[ {id:0,name:'已审核',value:'是'},  {id:1,name:'未审核',value:'否'} ],
      isendList:[ {id:0,name:'已核销',value:'是'},  {id:1,name:'未核销',value:'否'} ],
      tickettypeList:[ {id:0,name:'发票'},  {id:1,name:'收据'} ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ticketno: null,
        tickettype: null,
        useperson: null,
        usetime: null,
        startno: null,
        endno: null,
        prefix: null,
        invalid: null,
        buytime: null,
        isaudit: null,
        isend: null,
        demo: null,
        nextno: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ticketno: [
          { required: true, message: "票据本号不能为空", trigger: "blur" }
        ],
        tickettype: [
          { required: true, message: "票据类型不能为空", trigger: "change" }
        ],
        useperson: [
          { required: true, message: "领用人不能为空", trigger: "blur" }
        ],
        usetime: [
          { required: true, message: "领用时间不能为空", trigger: "blur" }
        ],
        startno: [
          { required: true, message: "票据号起不能为空", trigger: "blur" }
        ],
        endno: [
          { required: true, message: "票据号止不能为空", trigger: "blur" }
        ],
        prefix: [
          { required: true, message: "票据号前缀不能为空", trigger: "blur" }
        ],
        invalid: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ],
        buytime: [
          { required: true, message: "购买日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
     /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
         console.log(this.deptOptions);
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    /** 查询票据管理列表 */
    getList() {
      this.loading = true;
      listTicket(this.queryParams).then(response => {
        this.ticketList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ticketid: null,
        ticketno: null,
        tickettype: "发票",
        useperson: null,
        usetime: null,
        startno: null,
        endno: null,
        prefix: null,
        invalid: "有效",
        buytime: null,
        isaudit: "未审核",
        isend: "未核销",
        demo: null,
        nextno: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ticketid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加票据管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ticketid = row.ticketid || this.ids
      getTicket(ticketid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改票据管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ticketid != null) {
            updateTicket(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTicket(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ticketids = row.ticketid || this.ids;
      this.$modal.confirm('是否确认删除票据管理编号为"' + ticketids + '"的数据项？').then(function() {
        return delTicket(ticketids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/ticket/export', {
        ...this.queryParams
      }, `ticket_${new Date().getTime()}.xlsx`)
    },
  handleUser(val) {
       this.form.useperson=val.nickName;
       this.user=false;
       this.Person=false;
    },
    handleChange(data){
        //   for(let i=0;i<this.$refs['chargeperson'].getCheckedNodes().length;i++){
        //      if(this.$refs['chargeperson'].getCheckedNodes()[i].level==4){
        //         this.form.chargeperson=this.$refs['chargeperson'].getCheckedNodes()[i].data.label
        //      }
        //   }
         
        const  deptid=data.id;
        getUser(deptid).then(response => {
         this.getUserInfo=response.data;
         console.log(data);
       });
       this.user=true;
         
        // this.form.chargeperson=data.label;
          // this.Person=false;
        },
      selectPerson(){
          this.Person=true;
      },
      handleload(){
        this.visble=true;
      },
       tableRowClassName({row, rowIndex}) {
        if (rowIndex/2 === 0) {
          return 'warning-row';
        } else {
          return 'success-row';
        }
      }

  }
};
</script>
